<template>
  <div class="app-container">
    <el-form class="common-border pt10 mb10" :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="90px" label-position="right">
      <div>
        <el-form-item label="组织名称" prop="deptName">
          <el-input
            v-model="queryParams.dept.deptName"
            placeholder="请输入"
            clearable
            style="width:220px"
            :maxlength="50"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="经营地址" prop="deptDistrict">
<!--          <virtual-select-->
<!--            style="width:220px"-->
<!--            v-model="queryParams.dept.deptDistrict"-->
<!--            :list="allInfoOptions"-->
<!--            label="addressName"-->
<!--            value="districtId"-->
<!--            placeholder="请选择"-->
<!--            :keeps-params="10"-->
<!--            :is-concat="true"-->
<!--            :concat-symbol="'-'"-->
<!--            :merge-display="mergeDisplay"-->
<!--            :is-multiple="false"-->
<!--            :disabled="false"-->
<!--            :allow-create="false"-->
<!--          />-->
          <SelectAddress
            :AreaList="allInfoOptions"
            :districtValue="queryParams.dept.deptDistrict"
            @getFinalAddressDistrict="(e)=>this.$set(this.queryParams.dept,'deptDistrict',e)"
          ></SelectAddress>
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker
            style="width:220px"
            v-model="dateRange"
            type="datetimerange"
            value-format="yyyy-MM-dd HH:mm:ss"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间">
          </el-date-picker>
<!--          <el-date-picker-->
<!--            style="width:220px"-->
<!--            clearable-->
<!--            v-model="dateRange"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="请选择">-->
<!--          </el-date-picker>-->
        </el-form-item>
      </div>
      <div>
        <el-form-item label="手机账号" prop="phonenumber">
          <el-input
            style="width:220px"
            v-model="queryParams.phonenumber"
            placeholder="请输入"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="账号状态" prop="status">
          <el-select style="width:220px" v-model="queryParams.dept.status" clearable size="small" placeholder="请选择">
            <el-option label="启用" value="0"></el-option>
            <el-option label="停用" value="1"></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="开通时间" prop="openingTime">-->
<!--          <el-date-picker-->
<!--            style="width:220px"-->
<!--            v-model="queryParams.openingTime"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="请选择">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" size="mini" @click="handleQuery">查询</el-button>
        </el-form-item>
      </div>
    </el-form>
<!--    <el-row class="common-border pl5 pt5 pb5 mb10">-->
<!--      <el-col :span="1.5" class="mr10">-->
<!--        <el-button-->
<!--          icon="el-icon-plus"-->
<!--          type="primary"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5" class="mr10">-->
<!--        <el-button-->
<!--          icon="el-icon-download"-->
<!--          type="primary"-->
<!--          size="mini"-->
<!--          disabled-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--    </el-row>-->
    <el-table
      v-loading="loading"
      :data="list"
      v-table-height="{bottomOffset: 85,callback: height => tableHeight = height}"
      :height="tableHeight"
    >
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{(pageNum - 1) * pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="组织名称" min-width="120" align="center" prop="dept.deptName" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column label="服务商ID" min-width="120" align="center" prop="dept.deptId" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column label="组织类型" min-width="120" align="center" prop="dept.deptEntType" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ selectDictLabel(dict.type.dept_ent_type, scope.row.dept.deptEntType) }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="注册资本" min-width="120" align="center" prop="dept.deptId" :show-overflow-tooltip="true"></el-table-column>-->
      <el-table-column label="公司性质" min-width="120" align="center" prop="dept.deptNature" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ selectDictLabel(dict.type.customer_nature, scope.row.dept.deptNature) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="业务范围" min-width="120" align="center" prop="dept.deptBizScope" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column label="经营地址" min-width="120" align="center" prop="dept.deptDistrictName" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column label="联系人" min-width="120" align="center" prop="dept.leader" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column label="联系电话" min-width="120" align="center" prop="dept.phone" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="创建时间" min-width="120" align="center" prop="dept.createTime" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ scope.row.dept.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机账号" min-width="120" align="center" prop="phonenumber" :show-overflow-tooltip="true" />
      <el-table-column label="账号状态" min-width="120" align="center" prop="dept.status" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.dept.status === '0'"><el-tag type="success">启用</el-tag></span>
          <span v-else><el-tag type="info">停用</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column label="账号等级" min-width="120" align="center" prop="dept.level" :show-overflow-tooltip="true">
        完整版
      </el-table-column>
<!--      <el-table-column label="生效时长" min-width="120" align="center" prop="effectiveDuration" :show-overflow-tooltip="true" />-->
<!--      <el-table-column label="有效期" min-width="120" align="center" prop="periodOfValidity" :show-overflow-tooltip="true" />-->
      <el-table-column label="操作" align="center"  fixed="right"  width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleEdit(scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.dept.status === '0'"
            @click="handleStop(scope.row)"
          >停用</el-button>
          <el-button
            size="mini"
            type="text"
            v-else
            @click="handleEnable(scope.row)"
          >启用</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="pageNum" :limit.sync="pageSize" @pagination="onPage" />
    <Add ref="add" @closeDialog="onClose" />
    <Detail ref="detail" :key="`detail${autoKey}`" @close="autoKey++" />
    <Edit ref="edit" :areaList="areaList" :allInfoOptions="allInfoOptions" :key="`edit${autoKey}`" @close="onClose" />
  </div>
</template>

<script>
import { getUserList } from '@/api/login'
import Add from './components/add.vue'
import Detail from './components/detail.vue'
import Edit from './components/edit.vue'
import { getAllInfo,serviceUserList,updateBossEditDept,enableBossDept,disableBossDept,getAllInfoTree } from '@/api/login'

export default {
  name: "logisticsService",
  dicts: ['customer_nature', 'dept_type', 'dept_ent_type'],
  components: {
    Add,
    Detail,
    Edit
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      pageNum: 1,
      pageSize: 10,
      // 查询参数
      queryParams: {
        dept:{}
      },
      // 日期范围
      dateRange: [],
      tableHeight: 0,
      cardHeight: 0,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      autoKey: 0,
      allInfoOptions: [],
      areaList:[]
    };
  },
  created() {
    this.getList();
    this.getArea()
  },
  methods: {
    /** 查询登录日志列表 */
    getList() {
      this.loading = true;
      // serviceUserList({...this.queryParams,pageNum: this.pageNum,pageSize: this.pageSize}).then(response => {
      //   this.list = response.rows;
      //   this.total = response.total;
      //   this.loading = false;
      // });
      if(Array.isArray(this.dateRange))
      {
        this.queryParams.dept['createTimeBegin']=this.dateRange[0];
        this.queryParams.dept['createTimeEnd']=this.dateRange[1];
      }
      else
      {
        delete this.queryParams.dept.createTimeBegin;
        delete this.queryParams.dept.createTimeEnd;
      }
      getUserList(this.queryParams).then(res => {
        this.list = res.rows;
        this.total = res.total;
        this.loading = false;
      }).catch(err => {
        this.loading = false;
      })
    },

    onPage(e) {
      this.pageNum = e.page
      this.pageSize = e.limit
      this.getList()
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleAdd() {
      this.$refs.add.showDialog()
    },
    handleEdit(row) {
      this.$refs.edit.showDialog(row)
    },
    handleDetail(row) {
      this.$refs.detail.showDialog(row)
    },
    //停用
    handleStop(row){
      if(!row.dept.deptId)
      {
        this.$message.warning('当前账号未提交服务商信息，无法停用!')
        return
      }
      this.$confirm(`确定停用${row.dept.deptName}，账号${row.dept.phone}`).then(res => {
        this.loading = true
        //部门状态（0正常 1停用）
        disableBossDept(row.dept.deptId).then(res => {
          this.loading = false
          this.$message.success('已停用')
          this.getList()
        }).catch(err => {
          this.loading = false
        })
      })
    },
    //启用
    handleEnable(row){
      if(!row.dept.deptId)
      {
        this.$message.warning('当前账号未提交服务商信息，无法启用!')
        return
      }
      this.$confirm(`确定启用${row.dept.deptName}，账号${row.dept.phone}`).then(res => {
        this.loading = true
        //部门状态（0正常 1停用）
        enableBossDept(row.dept.deptId).then(res => {
          this.loading = false
          this.$message.success('已启用')
          this.getList()
        }).catch(err => {
          this.loading = false
        })
      })
    },
    onClose(e) {
      this.autoKey++
      this.getList()
    },
    handleDelete() {

    },
    mergeDisplay(val) {
      return `${val.provinceName}${val.addressName}`
    },
    getArea() {
      // getAllInfo().then(res => {
      //   this.areaList = res.data
      //   this.allInfoOptions = res.data
      // });
      getAllInfoTree().then(res => {
        this.areaList = res.data
        const deleteChildren = function(data) {
          for (let i in data) {
            if (data[i].children.length) {
              deleteChildren(data[i].children);
            }
            else delete data[i].children
          }
        }
        deleteChildren(this.areaList)
        this.allInfoOptions = JSON.parse(JSON.stringify(this.areaList))
        this.areaList.forEach(i=>i.disabled=true)
      });
    },
  }
};
</script>

